/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Company_controller;
import Controller.Headquarters_controller;
import Controller.Functionary_controller;
import Controller.Document_controller;
import Model.company_model;
import Model.Headquarters_model;
import Model.Functionary_model;
import Model.Document_model;
import Utilidades.Utilidades;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Molina
 */
public class Document_view extends javax.swing.JInternalFrame {
        static Utilidades utilids = new Utilidades();
        DefaultTableModel table;
        boolean editable = false;
        private DefaultTableModel datos = null;
        private int numPage = 1;
        /**
        * cantidad de pagina
        */
        private static int CANT_PAGE = -1;
        private static int NUM_RECORDS_PAGE = 4;
    /**
     * Creates new form Document_view
     */
    public Document_view() {
        initComponents();
        CargarTablaPaginada();
        company_model.cargarEmpresa(comboEmpresa);
        Headquarters_model.cargarSedes(comboSede);
        Functionary_model.cargarFuncionarios(comboFuncionario);
        utilids.cargarListas(comboTipoDocumento,6);
        utilids.cargarListas(comboModalidad,7);
        txtdevaluacion.setEnabled(false);
        txtnoDiasDefinitivo.setEnabled(false);
        txtMonto.setEditable(false);
        txtnoDiasPrestamo.setEnabled(false);
        txtTasa.setEnabled(false);
        comboModalidad.setEnabled(false);
        btnActualizar.setVisible(false);
        
        jLabel7.setVisible(false);
        txtbuscar_documento.setVisible(false);
        jButton1.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtbuscar_documento = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaDocumentos = new javax.swing.JTable();
        jLabelidDocumento = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JButton();
        btnActualizar = new javax.swing.JButton();
        bntNuevo = new javax.swing.JButton();
        comboEmpresa = new javax.swing.JComboBox();
        btnSiguiente = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        msgNumPagina = new javax.swing.JLabel();
        btnAnterior = new javax.swing.JButton();
        btnPrimero = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        comboSede = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txtvalorGiro = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        comboFuncionario = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        comboTipoDocumento = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        panelPrestamo = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        txtMonto = new javax.swing.JTextField();
        txtnoDiasPrestamo = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        comboModalidad = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        txtTasa = new javax.swing.JTextField();
        panelFondeo = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        txtdevaluacion = new javax.swing.JTextField();
        panelDefinitivo = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        txtnoDiasDefinitivo = new javax.swing.JTextField();
        txtFecha = new datechooser.beans.DateChooserCombo();

        setClosable(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gestión de Documentos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel7.setText("Código Documento");
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 120, 20));
        jPanel3.add(txtbuscar_documento, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 10, 160, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/find.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 10, 100, -1));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 670, 50));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Basicos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tablaDocumentos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Documento", "Empresa", "Sede", "Funcionario", "Fecha", "Valor Giro", "Tipo Documento"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaDocumentos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaDocumentosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaDocumentos);

        jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 380, 630, 110));
        jPanel2.add(jLabelidDocumento, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 30, 90, 20));

        jLabel5.setText("Fecha");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 60, 50, 20));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnAgregar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add.png"))); // NOI18N
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        jPanel4.add(btnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 110, 30));

        btnActualizar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/update.png"))); // NOI18N
        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });
        jPanel4.add(btnActualizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 110, 30));

        bntNuevo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bntNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/user_add.png"))); // NOI18N
        bntNuevo.setText("Nuevo");
        bntNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntNuevoActionPerformed(evt);
            }
        });
        jPanel4.add(bntNuevo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 110, 30));

        jPanel2.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 30, 130, 200));

        comboEmpresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(comboEmpresa, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, 110, -1));

        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_next.png"))); // NOI18N
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });
        jPanel2.add(btnSiguiente, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 490, 40, 30));

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_last.png"))); // NOI18N
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });
        jPanel2.add(btnUltimo, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 490, 40, 30));

        msgNumPagina.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        msgNumPagina.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        msgNumPagina.setText("# de #");
        jPanel2.add(msgNumPagina, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 490, 100, 20));

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_previous.png"))); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });
        jPanel2.add(btnAnterior, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 490, 40, 30));

        btnPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_first.png"))); // NOI18N
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });
        jPanel2.add(btnPrimero, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 490, 40, 30));

        jLabel8.setText("Valor Giro");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 90, 70, 20));

        jLabel9.setText("Sede");
        jPanel2.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 40, 20));

        jLabel10.setText("Empresa");
        jPanel2.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, 70, 20));

        comboSede.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(comboSede, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, 110, -1));

        jLabel11.setText("Código");
        jPanel2.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 70, 20));
        jPanel2.add(txtvalorGiro, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 90, 110, -1));

        jLabel12.setText("Funcionario");
        jPanel2.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, 70, 20));

        comboFuncionario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(comboFuncionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 120, 110, -1));

        jLabel13.setText("Tipo Documento");
        jPanel2.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 120, 100, 20));

        comboTipoDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        comboTipoDocumento.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboTipoDocumentoItemStateChanged(evt);
            }
        });
        jPanel2.add(comboTipoDocumento, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 120, 110, -1));
        jPanel2.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, 480, 10));

        panelPrestamo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Prestamo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        panelPrestamo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel14.setText("Monto");
        panelPrestamo.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 60, 20));
        panelPrestamo.add(txtMonto, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 110, -1));
        panelPrestamo.add(txtnoDiasPrestamo, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 110, -1));

        jLabel17.setText("No. Dias");
        panelPrestamo.add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, 60, 20));

        jLabel18.setText("Modalidad");
        panelPrestamo.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, 80, 20));

        comboModalidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        panelPrestamo.add(comboModalidad, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 50, 110, -1));

        jLabel19.setText("Tasa");
        panelPrestamo.add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 50, 60, 20));
        panelPrestamo.add(txtTasa, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 50, 110, -1));

        jPanel2.add(panelPrestamo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 280, 480, 90));

        panelFondeo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Fondeo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        panelFondeo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel15.setText("%  devaluacion");
        panelFondeo.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 100, 20));
        panelFondeo.add(txtdevaluacion, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 20, 110, -1));

        jPanel2.add(panelFondeo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 480, 50));

        panelDefinitivo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Definitivo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        panelDefinitivo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel16.setText("No.  Dias");
        panelDefinitivo.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 70, 20));
        panelDefinitivo.add(txtnoDiasDefinitivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 110, -1));

        jPanel2.add(panelDefinitivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 220, 480, 50));
        jPanel2.add(txtFecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 60, 110, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 670, 530));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 690, 640));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String buscar = txtbuscar_documento.getText();
        Document_controller document = Document_model.buscarDocumentos(buscar);

        if (txtbuscar_documento.getText().equals("")) {
            javax.swing.JOptionPane.showMessageDialog(this, "Digite el Registro a Buscar");
            return;
        } else {
            if (document != null) {
                MoverDatos(document);
                txtbuscar_documento.setText("");
            } else {
                JOptionPane.showMessageDialog(this, "No Encontro al Registro");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tablaDocumentosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaDocumentosMouseClicked
        // TODO add your handling code here:
//        editable = true;
//        if (evt.getClickCount() == 2) {
//            if (tablafuncionarios.getSelectedRowCount() >= 0) {
//                DefaultTableModel table = (DefaultTableModel) tablafuncionarios.getModel();
//                //Asigna a un variable el valor de la tabla seleccionada
//                int filamodificada = tablafuncionarios.getSelectedRow();
//                //toma los valores de la tabla y los agrega a las cajas de texto
//                jLabelidFuncionario.setText(table.getValueAt(filamodificada, 0).toString());
//                combo_tipdocumento.setSelectedItem(table.getValueAt(filamodificada, 1).toString());
//                txtdocumento_funcionario.setText(table.getValueAt(filamodificada, 2).toString());
//                txtnombre_funcionario.setText(table.getValueAt(filamodificada, 3).toString());
//                txtapellido_funcionario.setText(table.getValueAt(filamodificada, 4).toString());
//                combo_cargo.setSelectedItem(table.getValueAt(filamodificada, 5).toString());
//                txttelefono_funcionario.setText(table.getValueAt(filamodificada, 6).toString());
//                txtemail_funcionario.setText(table.getValueAt(filamodificada, 7).toString());
//                txtextension_funcionario.setText(table.getValueAt(filamodificada, 8).toString());
//                combo_estado.setSelectedItem(table.getValueAt(filamodificada, 9).toString());
//            }
//        }
    }//GEN-LAST:event_tablaDocumentosMouseClicked

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        // TODO add your handling code here:
        String datos[] = new String[9];
        String acum = "";
        String noDias = "0";
        String valorgiro = "0";
        String devaluacion = "0";
        String monto = "0";        
        String tasa = "0";
        
        if(txtnoDiasDefinitivo.getText().equals("")){
            noDias = txtnoDiasPrestamo.getText();
        } 
        if(txtnoDiasPrestamo.getText().equals("")){
            noDias = txtnoDiasDefinitivo.getText();
        } 
        if(txtnoDiasPrestamo.getText().equals("") && txtnoDiasDefinitivo.getText().equals("")){
            noDias = "0";
        }
        
        if(txtvalorGiro.getText().equals("")){
            valorgiro = "0";
        }else{
            valorgiro = txtvalorGiro.getText();
        }
        
        if(txtdevaluacion.getText().equals("")){
            devaluacion = "0";
        }else{
            devaluacion = txtdevaluacion.getText();
        }
        
        if(txtMonto.getText().equals("")){
            monto = "0";
        }else{
            monto = txtMonto.getText();
        }
        
        if(txtTasa.getText().equals("")){
            tasa = "0";
        }else{
            tasa = txtTasa.getText();
        }
         
        String empresa = comboEmpresa.getSelectedItem().toString();
        String sede = comboSede.getSelectedItem().toString();
        String funcionario = comboFuncionario.getSelectedItem().toString();
        String fecha = txtFecha.getText();
        String tipdocument = comboTipoDocumento.getSelectedItem().toString();
        
        //Capturo el id de la Empresa, borrando la primera palabra del combo de empresa
        String nombreempresa = utilids.borrarPrimeraPalabra(empresa);
        String idempresa = Utilidades.Ppalabra(empresa);
        
        //Capturo el id de la Sede, borrando la primera palabra del combo de sede
        String nombresede = utilids.borrarPrimeraPalabra(sede);
        String idsede = Utilidades.Ppalabra(sede);
        
        //Capturo el id del Funcionario, borrando la primera palabra del combo de funcionario
        String nombrefuncionario = utilids.borrarPrimeraPalabra(funcionario);
        String idefuncionario = Utilidades.Ppalabra(funcionario);
        
        String modalidad = comboModalidad.getSelectedItem().toString();
        
        if(modalidad.equals("[Seleccione]")){
            modalidad = "";
        }else{
            modalidad = comboModalidad.getSelectedItem().toString();
        }

        Document_controller document = new Document_controller("", idsede, idempresa, idefuncionario, fecha, valorgiro,
                tipdocument, devaluacion, noDias, monto, tasa, modalidad);
        try {
            if (comboEmpresa.getSelectedItem().equals("[Seleccione]") || comboSede.getSelectedItem().equals("[Seleccione]") || 
                comboFuncionario.getSelectedItem().equals("[Seleccione]") || txtFecha.getText().equals("") || txtvalorGiro.getText().equals("")
                    || comboTipoDocumento.getSelectedItem().toString().equals("[Seleccione]")) {
                acum += "Algun campo le quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
            } 
            if (comboTipoDocumento.getSelectedItem().toString().equals("Fondeo")) {
                if (txtdevaluacion.getText().equals("")) {
                acum += "Elcampo % Devaluacion quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
                } 
            }
            if (comboTipoDocumento.getSelectedItem().toString().equals("Definitivo")) {
                if (txtnoDiasDefinitivo.getText().equals("")) {
                acum += "El campo No. Dias del Documento Definitivo quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
                } 
            }
            if (comboTipoDocumento.getSelectedItem().toString().equals("Prestamo")) {
                if (txtnoDiasPrestamo.getText().equals("") || txtMonto.getText().equals("") || comboModalidad.getSelectedItem().equals("[Seleccione]")) {
                acum += "El campo del Documento de Prestamo quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
                } 
            }
            else {
                System.out.println("Entro");
                if (Document_model.adicionarDocumento(document)) {
                    table = (DefaultTableModel) tablaDocumentos.getModel();
                    datos[0] = nombreempresa;
                    datos[1] = nombresede;
                    datos[2] = nombrefuncionario;
                    datos[3] = this.txtFecha.getText();
                    this.txtFecha.setText(null);
                    datos[5] = this.txtvalorGiro.getText();
                    this.txtvalorGiro.setText(null);
                    datos[6] = this.comboTipoDocumento.getSelectedItem().toString();
                    this.comboTipoDocumento.setSelectedItem(null);
                    table.addRow(datos);
                    limpiarTabla();
                    CargarTablaPaginada();
                    //JOptionPane.showMessageDialog(this, "Registro Adicionado");
                } else {
                    JOptionPane.showMessageDialog(this, "Registro No Adicionado", "", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        // TODO add your handling code here:
//        String id = jLabelidFuncionario.getText();
//        String tipdocumento = combo_tipdocumento.getSelectedItem().toString();
//        String documentofuncionario = txtdocumento_funcionario.getText();
//        String nombrefuncionario = txtnombre_funcionario.getText();
//        String apellidofuncionario = txtapellido_funcionario.getText();
//        String cargo = combo_cargo.getSelectedItem().toString();
//        String telefonofuncionario = txttelefono_funcionario.getText();
//        String emailfuncionario = txtemail_funcionario.getText();
//        String extensionfuncionario = txtextension_funcionario.getText();
//        String estado = combo_estado.getSelectedItem().toString();
//
//        String acum = "";
//        try {
//            if (combo_tipdocumento.getSelectedItem().equals("[Seleccione]") || txtdocumento_funcionario.getText().equals("") || txtnombre_funcionario.getText().equals("")
//                    || txtapellido_funcionario.getText().equals("") || combo_cargo.getSelectedItem().equals("[Seleccione]") || txttelefono_funcionario.getText().equals("")
//                    || txtemail_funcionario.getText().equals("") || txtextension_funcionario.getText().equals("")
//                    || combo_estado.getSelectedItem().equals("[Seleccione]")) {
//                acum += "Algun campo le quedo incompleto, por favor llenelo" + ".\t\n";
//                JOptionPane.showMessageDialog(null, acum);
//                return;
//            }
//            Functionary_controller funcionario = new Functionary_controller(id, tipdocumento, documentofuncionario,
//                    nombrefuncionario, apellidofuncionario, cargo, telefonofuncionario, emailfuncionario, extensionfuncionario, estado);
//            Functionary_model.modificarFuncionario(funcionario);
//            limpiarTabla();
//            CargarTablaPaginada();
//            //JOptionPane.showMessageDialog(this, "Registro Actualizado");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void bntNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntNuevoActionPerformed
        // TODO add your handling code here:
        jLabelidDocumento.setText("");
        comboEmpresa.setSelectedItem("[Seleccione]");
        comboSede.setSelectedItem("[Seleccione]");
        comboFuncionario.setSelectedItem("[Seleccione]");
        txtvalorGiro.setText("");
        txtFecha.setSelectedDate(null);
        comboTipoDocumento.setSelectedItem("[Seleccione]");
        txtdevaluacion.setText("");
        txtnoDiasDefinitivo.setText("");
        txtMonto.setText("");
        txtnoDiasPrestamo.setText("");
        comboModalidad.setSelectedItem("[Seleccione]");
        txtTasa.setText("");
    }//GEN-LAST:event_bntNuevoActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        // TODO add your handling code here:
        if (numPage < CANT_PAGE) {
            numPage++;
        }
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        // TODO add your handling code here:
        numPage = CANT_PAGE;
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        // TODO add your handling code here:
        if (numPage > 1) {
            numPage--;
        }
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        // TODO add your handling code here:
        numPage = 1;
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void comboTipoDocumentoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboTipoDocumentoItemStateChanged
        // TODO add your handling code here:
        if(comboTipoDocumento.getSelectedItem().toString().equals("Fondeo"))
        {
            txtdevaluacion.setEnabled(true);
            
            txtnoDiasDefinitivo.setText("");
            txtnoDiasDefinitivo.setEnabled(false);
            txtMonto.setText("");
            txtMonto.setEditable(false);
            txtnoDiasPrestamo.setText("");
            txtnoDiasPrestamo.setEnabled(false);
            comboModalidad.setSelectedItem("[Seleccione]");
            comboModalidad.setEnabled(false);
            txtTasa.setText("");
            txtTasa.setEnabled(false);
        }
        else if(comboTipoDocumento.getSelectedItem().toString().equals("Definitivo"))
        {
            txtnoDiasDefinitivo.setEnabled(true);
            
            txtdevaluacion.setText("");
            txtdevaluacion.setEnabled(false);
            txtMonto.setText("");
            txtMonto.setEditable(false);
            txtnoDiasPrestamo.setText("");
            txtnoDiasPrestamo.setEnabled(false);
            txtTasa.setText("");
            txtTasa.setEnabled(false);
            comboModalidad.setSelectedItem("[Seleccione]");
            comboModalidad.setEnabled(false);
        }
        else
        {
            txtMonto.setEditable(true);
            txtnoDiasPrestamo.setEnabled(true);
            comboModalidad.setEnabled(true);
            txtTasa.setEnabled(true);
            
            txtdevaluacion.setText("");
            txtdevaluacion.setEnabled(false);
            txtnoDiasDefinitivo.setText("");
            txtnoDiasDefinitivo.setEnabled(false);
        }
    }//GEN-LAST:event_comboTipoDocumentoItemStateChanged

    public void MoverDatos(Document_controller document) {
        comboEmpresa.setSelectedItem(document.getEmpresas_emp_numero().toString());
        comboSede.setSelectedItem(document.getSedes_sed_numero().toString());
        comboFuncionario.setSelectedItem(document.getFuncionarios_fun_numero().toString());
        txtvalorGiro.setText(document.getDoc_valorgiro());
        txtFecha.setText(document.getDoc_fechaelab());
        comboTipoDocumento.setSelectedItem(document.getDoc_tipodocumento().toString());
        txtdevaluacion.setText(document.getDoc_porcentajedevaluacion());
          
        
        txtMonto.setText(document.getDoc_monto());
        txtTasa.setText(document.getDoc_tasa());
        comboModalidad.setSelectedItem(document.getDoc_modalidad().toString());

//        if(comboEmpresa.getSelectedItem().equals(16)){
//            txtnoDiasPrestamo.setText(document.getDoc_numerodias());
//        }
//        if(comboEmpresa.getSelectedItem().equals(17)){
//            txtnoDiasDefinitivo.setText(document.getDoc_numerodias());
//        }
    }
    
    public void CargarTablaPaginada() {
        try {
            ArrayList<Object[]> data = new ArrayList<Object[]>();
            Utilidades pag = new Utilidades();
            String consulta = "SELECT doc_numero, emp_nombre, sed_nombre, CONCAT( fun_nombre,' ', fun_apellido) AS nombre, doc_fechaelab, doc_valorgiro, doc_tipodocumento "
                    + " FROM Documentos, Empresas, Sedes, Funcionarios "
                    + " WHERE Documentos.Sedes_sed_numero = Sedes.sed_numero "
                    + " AND Documentos.Empresas_emp_numero = Empresas.emp_numero "
                    + " AND Documentos.Funcionarios_fun_numero = Funcionarios.fun_numero";
            //System.out.print(consulta);
            List lista = pag.ejecutaQueryPaginada(consulta, numPage, NUM_RECORDS_PAGE);
            Integer numeroRegistros = (Integer) lista.get(0);
            float tempNuumPage = (float) numeroRegistros / NUM_RECORDS_PAGE;
            CANT_PAGE = Math.round(tempNuumPage);//((int)tempNuumPage + (Math.round(tempNuumPage)%2 == 0 ? 0 : 1));
            // System.out.println("NUMERO DE REGISTROS TOTALES:" + numeroRegistros.toString());
            msgNumPagina.setText("Pagina " + numPage + " de " + CANT_PAGE);
            /*
             * for (int i = 1; i < lista.size(); i++) { List lista1 = (List)
             * lista.get(i); System.out.println(lista1.toString()); }
             */
            ArrayList unRegistro = null;
            for (int i = 2; i < lista.size(); i++) {
                unRegistro = (ArrayList) lista.get(i);
                Object[] rows = new Object[unRegistro.size()];
                for (int j = 0; j < unRegistro.size(); j++) {
                    rows[j] = unRegistro.get(j);
                }
                data.add(rows);
            }
            table = (DefaultTableModel) this.tablaDocumentos.getModel();
            for (int i = 0; i < data.size(); i++) {
                table.addRow(data.get(i));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e.getMessage());
        }
    }

    public void limpiarTabla() {
        table = (DefaultTableModel) tablaDocumentos.getModel();
        try {
            int size = table.getRowCount();
            for (int i = size - 1; i >= 0; i--) {
                table.removeRow(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntNuevo;
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox comboEmpresa;
    private javax.swing.JComboBox comboFuncionario;
    private javax.swing.JComboBox comboModalidad;
    private javax.swing.JComboBox comboSede;
    private javax.swing.JComboBox comboTipoDocumento;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelidDocumento;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel msgNumPagina;
    private javax.swing.JPanel panelDefinitivo;
    private javax.swing.JPanel panelFondeo;
    private javax.swing.JPanel panelPrestamo;
    private javax.swing.JTable tablaDocumentos;
    private datechooser.beans.DateChooserCombo txtFecha;
    private javax.swing.JTextField txtMonto;
    private javax.swing.JTextField txtTasa;
    private javax.swing.JTextField txtbuscar_documento;
    private javax.swing.JTextField txtdevaluacion;
    private javax.swing.JTextField txtnoDiasDefinitivo;
    private javax.swing.JTextField txtnoDiasPrestamo;
    private javax.swing.JTextField txtvalorGiro;
    // End of variables declaration//GEN-END:variables
}
